package com.cefetmg.mmc.mrcpsp.process.impl;

import java.io.File;

import org.apache.commons.lang.StringUtils;

import com.cefetmg.mmc.mrcpsp.model.enums.EnumExecutionTypes;
import com.cefetmg.mmc.mrcpsp.model.main.Project;
import com.cefetmg.mmc.mrcpsp.process.ResultsProcessor;
import com.cefetmg.mmc.mrcpsp.utils.FileUtils;
import com.cefetmg.mmc.mrcpsp.utils.LogUtils;
import com.cefetmg.mmc.mrcpsp.utils.PropertyConstants;
import com.cefetmg.mmc.mrcpsp.utils.PropertyManager;
import com.cefetmg.mmc.mrcpsp.utils.UrlUtils;

public class ResultsProcessorImpl implements ResultsProcessor {
	
	private Integer lowerMakespan;	
		
	@Override
	public void writeResultsToOneInstance(Project project) {		
		String pathFile = UrlUtils.getInstance().getUrlForResultsFileToOneInstance(project.getFileName());
		boolean append = true;
		String data = project.getFileName() +  " MAKESPAN: " + Integer.toString(project.getMakespan()) + LogUtils.getINSTANCE_STATUS() + "\n";
		
		FileUtils.writeToFile(new File(pathFile), data, append);
	}

	@Override
	public void getMakespanFromScheduledJobs(Project project, boolean success) {		
		if (success) {
			Integer lastIndex = project.getStaggeredJobs().size() - 1;
			Integer makespan = project.getStaggeredJobs().get(lastIndex).getEndTime();
			
			project.setMakespan(makespan);
		} else {			
			LogUtils.setINSTANCE_STATUS(" - Check results.");
			project.setInstanceResultStatus(" - Check results.");
			project.setMakespan(PropertyConstants.INSTANCE_MAKESPAN_ERROR);
		}
	}

	@Override
	public void checkLowerMakespan(Integer makespan) {
		if (lowerMakespan == null || lowerMakespan == PropertyConstants.INSTANCE_MAKESPAN_ERROR) {
			lowerMakespan = makespan;
		} else {			
			if (makespan < lowerMakespan) {
				lowerMakespan = makespan;
			}
		}
	}

	@Override
	public void writeLowerMakespanToOneInstance(String fileName) {
		String pathFile = UrlUtils.getInstance().getUrlForResultsFileToOneInstance(fileName);
		boolean append = true;
		String data = PropertyManager.getInstance().getProperty(PropertyConstants.INSTANCE_FILE) + " LOWER MAKESPAN: " + Integer.toString(lowerMakespan) + LogUtils.getINSTANCE_STATUS() + "\n";
		
		FileUtils.writeToFile(new File(pathFile), data, append);		
	}

	@Override
	public void writeRunningTimeToResultFile(String data, String fileName) {
		String pathFile = UrlUtils.getInstance().getUrlForResultsFileToOneInstance(fileName);
		boolean append = true;
		
		FileUtils.writeToFile(new File(pathFile), data, append);	
	}

	@Override
	public void checkExecutionTypeToGenerateResults(Project project) {
		String executionType = UrlUtils.getInstance().getExecutionType();
				
		if (StringUtils.equals(executionType, EnumExecutionTypes.ONE_FILE.getName())) {		
			writeResultsToOneInstance(project);
		} else if (StringUtils.equals(executionType, EnumExecutionTypes.ALL.getName())) {
			writeResultsAllFile(project);
		} 
		
	}

	@Override
	public void writeResultsAllFile(Project project) {
		String pathFile = UrlUtils.getInstance().getUrlForResultsFileToAllInstances();
		boolean append = true;
		String data = project.getFileName() +  " MAKESPAN: " + Integer.toString(project.getMakespan()) + project.getInstanceResultStatus() + "\n";
		
		FileUtils.writeToFile(new File(pathFile), data, append);		
	}

	@Override
	public void writeRunningTimeToResultFileAllInstances(String data) {
		String pathFile = UrlUtils.getInstance().getUrlForResultsFileToAllInstances();
		boolean append = true;
		
		FileUtils.writeToFile(new File(pathFile), data, append);		
	}

	@Override
	public void writeLowerMakespanToAllInstances(String fileName) {
		String pathFile = UrlUtils.getInstance().getUrlForResultsFileToAllInstances();
		boolean append = true;
		String instanceResultStatus = "";
		
		if (lowerMakespan == PropertyConstants.INSTANCE_MAKESPAN_ERROR) {
			instanceResultStatus = " - Check results.";
		}
		
		String data = fileName + " LOWER MAKESPAN: " + Integer.toString(lowerMakespan) + instanceResultStatus + "\n";
		
		FileUtils.writeToFile(new File(pathFile), data, append);			
	}	

	public void setLowerMakespan(Integer lowerMakespan) {
		this.lowerMakespan = lowerMakespan;
	}	

}
